/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.sx.water.util;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *表工具类
 */
public class TableUtil {
    /**
     * 获取时间戳  根据时间 /间隔
     * @param num 1/3/6/12/24
     * @param time yymmddHH
     * @return  表后缀 与 起始时间 结束时间
     */
    public static Map<String,String> getTimeComparison(int num, String time){
        StringBuffer stringBuffer=new StringBuffer();
        StringBuffer stringBuffer2=new StringBuffer();
        Date date= DateUtils.stringToDate(time,DateUtils.DATE_TIME_PATTERN_1);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        Map<String,String> map=new HashMap<>();

        //根据传值获取3小时前的时间
        Calendar calendar2=Calendar.getInstance();
        calendar2.setTime(date);
        calendar2.add(Calendar.HOUR_OF_DAY,-(num-1));
        Date date1=calendar2.getTime();
        DecimalFormat df=new DecimalFormat("00");
        //判断是否跨日/月/年
        //判断第一个传值时间
        if(DateUtils.isLastDayOfMonth(date)){
            //是，判断当前时间 是否超过 20点
            if(calendar.get(Calendar.HOUR_OF_DAY) <= 20){
                stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format(calendar.get(Calendar.MONTH)+1));
            }else{
                calendar.add(Calendar.MONTH,1);
                stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format(calendar.get(Calendar.MONTH)+1));
            }
        }else {
            stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format(calendar.get(Calendar.MONTH)+1));
        }
        //判断第二个传值时间
        if(DateUtils.isLastDayOfMonth(date1)){
            //是，判断当前时间 是否超过 20点
            if(calendar2.get(Calendar.HOUR_OF_DAY) <= 20){
                stringBuffer2.append(calendar2.get(Calendar.YEAR)).append(df.format(calendar2.get(Calendar.MONTH)+1));
            }else{
                calendar2.add(Calendar.MONTH,1);
                stringBuffer2.append(calendar2.get(Calendar.YEAR)).append(df.format(calendar2.get(Calendar.MONTH)+1));
            }
        }else {
            stringBuffer2.append(calendar2.get(Calendar.YEAR)).append(df.format(calendar2.get(Calendar.MONTH)+1));
        }
        //两个时间在同一张表中
        if(stringBuffer.toString().equals(stringBuffer2.toString())){
            //获取表后缀
            map.put("tableSuffix",stringBuffer.toString());
            map.put("startTime",DateUtils.format(date1,DateUtils.DATE_TIME_PATTERN_1));
            map.put("endTime",time);
            map.put("tableSuffix2",null);
            map.put("startTime2",null);
            map.put("endTime2",null);
        }else {
            //两时间不在同一张表
            //获取获取跨表的时间
            Calendar calendar3=Calendar.getInstance();
            calendar3.setTime(date1);
            StringBuffer midTime=new StringBuffer();
            StringBuffer midTime2=new StringBuffer();
            //如果传值时间 是月的最后一天
            if(DateUtils.isLastDayOfMonth(date1)){
                String month=df.format(calendar3.get(Calendar.MONTH)+1);
                String day=df.format(calendar3.get(Calendar.DAY_OF_MONTH));

                midTime.append(String.valueOf(calendar3.get(Calendar.YEAR)).substring(2)).append(month).append(day).append("21");
                midTime2.append(String.valueOf(calendar3.get(Calendar.YEAR)).substring(2)).append(month).append(day).append("20");


            }else{
                //如果不是最后一天
                calendar3.set(Calendar.DAY_OF_MONTH, 1);
                calendar3.add(Calendar.DATE, -1);

                String month=df.format(calendar3.get(Calendar.MONTH)+1);
                String day=df.format(calendar3.get(Calendar.DAY_OF_MONTH));

                midTime.append(String.valueOf(calendar3.get(Calendar.YEAR)).substring(2)).append(month).append(day).append("21");
                midTime2.append(String.valueOf(calendar3.get(Calendar.YEAR)).substring(2)).append(month).append(day).append("20");

            }
            map.put("tableSuffix",stringBuffer.toString());
            map.put("startTime",midTime.toString());
            map.put("endTime",time);
            map.put("tableSuffix2",stringBuffer2.toString());
            map.put("startTime2",DateUtils.format(date1,DateUtils.DATE_TIME_PATTERN_1));
            map.put("endTime2",midTime2.toString());

        }

        return map;
    }

    public static void main(String[] args) {
        TableUtil.getTimeComparison(3,"20060119");
    }
}
